﻿using Developer.Entity;
using Developer.Service;
using Super.Framework.Common;
using Super.Framework.Common.Web;
using Super.Framework.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Developer.Web.Areas.Admin.Controllers
{
    public class RepairController : BaseController
    {
        #region Type维修类型
        /// <summary>
        /// 首页列表/ 维修类型列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
           
            var cities = RepairTypeService.GetAll().Where(a => a.IsAbort == false);
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new RepairType
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     RepairBrand = a.RepairBrand
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }


        public ActionResult EditType()
        {
            var typeId = Query.Value("typeId").ToInt();
            
            var RepairType = new RepairType();
            if (typeId > 0)
            {
                RepairType = RepairTypeService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(RepairType);
        }

        [HttpPost]
        public ActionResult EditType(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var repairType = new RepairType();
            if (typeId > 0)
            {
                repairType = RepairTypeService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (repairType != null)
                {
                    repairType.Name = typename;
                    RepairTypeService.Change(repairType);
                }
            }
            else
            {
                repairType.Name = typename;
                repairType.IsAbort = false;
                repairType.CreateTime = DateTime.Now;
                RepairTypeService.Add(repairType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/repair/edittype?typeId=" + typeId );
        }

        public ActionResult DeleteType()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                RepairTypeService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/repair/type");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/repair/type");
        }
        #endregion

        #region Brand维修品牌
        public ActionResult Brand()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = RepairBrandService.GetAll().Where(a => a.IsAbort == false);
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new RepairBrand
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     RepairTypeId = a.RepairTypeId,
                     RepairType = a.RepairType,
                     RepairModel = a.RepairModel
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }


        public ActionResult EditBrand()
        {
            var typeId = Query.Value("typeId").ToInt();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            var RepairType = new RepairBrand();
            if (typeId > 0)
            {
                RepairType = RepairBrandService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(RepairType);
        }

        [HttpPost]
        public ActionResult EditBrand(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var repairParentId = Query.Value("parentId").ToInt();  //父类id
            var repairType = new RepairBrand();
            if (typeId > 0)
            {
                repairType = RepairBrandService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (repairType != null)
                {
                    repairType.Name = typename;
                    if(repairParentId > 0)
                    {
                        repairType.RepairTypeId = repairParentId;
                    }
                    RepairBrandService.Change(repairType);
                }
            }
            else
            {
                repairType.Name = typename;
                repairType.CreateTime = DateTime.Now;
                repairType.IsAbort = false;
                repairType.RepairTypeId = repairParentId;

                var last_sequence = RepairBrandService.GetAll().Where(a => a.RepairTypeId == repairParentId).OrderByDescending(a => a.Sequence);
                if (last_sequence.Count() > 0)
                {
                    repairType.Sequence = last_sequence.FirstOrDefault().Sequence++;

                }
                else
                {
                    repairType.Sequence = 1;
                }
                RepairBrandService.Add(repairType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/repair/editbrand?typeId=" + typeId + "&remain=" + repairParentId);
        }

        public ActionResult DeleteBrand()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                RepairBrandService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/repair/brand");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/repair/brand");
        }
        #endregion

        #region Model维修型号
        public ActionResult Model()
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = RepairModelService.GetAll().Where(a => a.IsAbort == false);
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new RepairModel
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     RepairBrandId = a.RepairBrandId,
                     RepairBrand = a.RepairBrand,
                     RepairColor = a.RepairColor
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
            return View(items);
        }


        public ActionResult EditModel()
        {
            var typeId = Query.Value("typeId").ToInt();
            var RepairType = new RepairModel();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            if (typeId > 0)
            {
                RepairType = RepairModelService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(RepairType);
        }

        [HttpPost]
        public ActionResult EditModel(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var repairParentId = Query.Value("parentId").ToInt();  //父类id
            var repairType = new RepairModel();
            if (typeId > 0)
            {
                repairType = RepairModelService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (repairType != null)
                {
                    repairType.Name = typename;
                    if (repairParentId != 0)
                    {
                        repairType.RepairBrandId = repairParentId;
                    }
                    RepairModelService.Change(repairType);
                }
            }
            else
            {
                repairType.Name = typename;
                repairType.CreateTime = DateTime.Now;
                repairType.RepairBrandId = repairParentId;
                repairType.IsAbort = false;                             //修改完改回

                var last_sequence = RepairModelService.GetAll().Where(a => a.RepairBrandId == repairParentId).OrderByDescending(a => a.Sequence);
                if (last_sequence.Count() > 0)
                {
                    repairType.Sequence = last_sequence.FirstOrDefault().Sequence++;

                }
                else
                {
                    repairType.Sequence = 1;
                }
                RepairModelService.Add(repairType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/repair/editmodel?typeId=" + typeId + "&remain=" + repairParentId);
        }

        public ActionResult DeleteModel()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                RepairModelService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/repair/model");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/repair/model");
        }
        #endregion

        #region Color维修颜色
        public ActionResult Color(int? mid)
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = RepairColorService.GetAll().Where(a => a.IsAbort == false);

            if (mid.HasValue)               //根据mid（ModelId）进行筛选
            {
                cities = cities.Where(a => a.RepairModelId == mid);
                ViewBag.mid = mid;
            }
            else
            {
                ViewBag.mid = 0;
            }


            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new RepairColor
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     RepairModelId = a.RepairModelId,
                     Price = a.Price,
                     RepairModel = a.RepairModel,
                     RepairProblem = a.RepairProblem,
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
          
            return View(items);
        }


        public ActionResult EditColor()
        {
            var typeId = Query.Value("typeId").ToInt();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            var RepairType = new RepairColor();
            if (typeId > 0)
            {
                RepairType = RepairColorService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(RepairType);
        }

        [HttpPost]
        public ActionResult EditColor(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var price = Query.Value("price").ToInt();
            var repairParentId = Query.Value("parentId").ToInt();  //父类id
            var repairType = new RepairColor();
            if (typeId > 0)
            {
                repairType = RepairColorService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (repairType != null)
                {
                    repairType.Name = typename;
                    repairType.Price = price;
                    if (repairParentId != 0)
                    {
                        repairType.RepairModelId = repairParentId;
                    }
                    RepairColorService.Change(repairType);
                }
            }
            else
            {
                repairType.Name = typename;
                repairType.CreateTime = DateTime.Now;
                repairType.Price = price;
                repairType.RepairModelId = repairParentId;
                repairType.IsAbort = false;                             //修改完改回
                RepairColorService.Add(repairType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/repair/editColor?typeId=" + typeId);
        }

        public ActionResult DeleteColor()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                RepairColorService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/repair/Color");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/repair/Color");
        }
        #endregion

        #region Problem维修故障
        public ActionResult Problem(int? mid,int? cid)
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;
            
            var cities = RepairProblemService.GetAll().Where(a => a.IsAbort == false);

            if (mid.HasValue)
            {
                cities = cities.Where(a => a.RepairColor.RepairModelId == mid);
                if (cid.HasValue)
                {
                    cities = cities.Where(a => a.RepairColorId == cid);
                    ViewBag.cid = cid;
                }
                else
                {
                    ViewBag.cid = 0;
                }
                ViewBag.mid = mid;
            }
            else
            {
                ViewBag.mid = 0;
            }

            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new RepairProblem
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     RepairColorId=a.RepairColorId,
                     RepairColor = a.RepairColor,
                     RepairProblemDetail = a.RepairProblemDetail
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
          
            return View(items);
        }


        public ActionResult EditProblem()
        {
            var typeId = Query.Value("typeId").ToInt();
            var RepairType = new RepairProblem();
            var remain = Query.Value("remain").ToInt();
            if (typeId > 0)
            {
                RepairType = RepairProblemService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            ViewBag.remain = remain;
            return View(RepairType);
        }

        [HttpPost]
        public ActionResult EditProblem(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var repairParentId = Query.Value("parentId").ToInt();  //父类id
            var repairType = new RepairProblem();
            if (typeId > 0)
            {
                repairType = RepairProblemService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (repairType != null)
                {
                    repairType.Name = typename;
                    if (repairParentId != 0)
                    {
                        repairType.RepairColorId = repairParentId;
                    }
                    RepairProblemService.Change(repairType);
                }
            }
            else
            {
                repairType.Name = typename;
                repairType.CreateTime = DateTime.Now;
                repairType.Id = repairParentId;
                repairType.IsAbort = false;
                repairType.RepairColorId = repairParentId;

                var last_sequence = RepairProblemService.GetAll().Where(a => a.RepairColorId == repairParentId).OrderByDescending(a => a.Sequence);
                if (last_sequence.Count() > 0)
                {
                    repairType.Sequence = last_sequence.FirstOrDefault().Sequence++;

                }
                else
                {
                    repairType.Sequence = 1;
                }
                RepairProblemService.Add(repairType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/repair/editProblem?typeId=" + typeId + "&remain=" + repairParentId);
        }

        public ActionResult DeleteProblem()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                RepairProblemService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/repair/Problem");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/repair/Problem");
        }
        #endregion

        #region ProblemDetail维修故障详情
        public ActionResult ProblemDetail(int? mid,int? cid,int? pid)
        {
            var pageIndex = Query.Value("page").ToInt(1);
            var key = Query.Value("name");
            int pageCount;
            int itemCount;

            var cities = RepairProblemDetailService.GetAll().Where(a => a.IsAbort == false);
            if (mid.HasValue)               //筛选Start
            {
                cities = cities.Where(a => a.RepairProblem.RepairColor.RepairModelId == mid);
                ViewBag.mid = mid;
                if (cid.HasValue)
                {
                    cities = cities.Where(a => a.RepairProblem.RepairColorId == cid);
                    ViewBag.cid = cid;
                    if (pid.HasValue)
                    {
                        cities = cities.Where(a => a.RepairProblemId == pid);
                        ViewBag.pid = pid;
                    }
                    else
                    {
                        ViewBag.pid = 0;
                    }
                }
                else
                {
                    ViewBag.cid = 0;
                }
            }
            else
            {
                ViewBag.mid = 0;
            }                                    //筛选End
            if (key.IsNotNullOrEmpty())
            {
                cities = cities.Where(a => a.Name.Contains(key));
                ViewBag.Name = key;
            }
            var items = cities.OrderByDescending(u => u.CreateTime)
                 .Page(out itemCount, out pageCount, pageSize: PageSize, pageIndex: pageIndex).Select(a => new RepairProblemDetail
                 {
                     Id = a.Id,
                     CreateTime = a.CreateTime,
                     Name = a.Name,
                     IsAbort = a.IsAbort,
                     Price = a.Price,
                     RepairProblem = a.RepairProblem,
                     RepairProblemId = a.RepairProblemId
                 });

            ViewBag.pageCount = pageCount;
            ViewBag.itemCount = itemCount;
           
            return View(items);
        }


        public ActionResult EditProblemDetail()
        {
            var typeId = Query.Value("typeId").ToInt();
            var remain = Query.Value("remain").ToInt();
            ViewBag.remain = remain;
            var RepairType = new RepairProblemDetail();
            if (typeId > 0)
            {
                RepairType = RepairProblemDetailService.GetAll().FirstOrDefault(c => c.Id == typeId);
            }
            return View(RepairType);
        }

        [HttpPost]
        public ActionResult EditProblemDetail(int? z)
        {
            var typename = Query.Value("typename");
            var typeId = Query.Value("typeId").ToInt();
            var price = Query.Value("price").ToInt();
            var repairParentId = Query.Value("parentId").ToInt();  //父类id
            var repairType = new RepairProblemDetail();
            if (typeId > 0)
            {
                repairType = RepairProblemDetailService.GetAll().FirstOrDefault(c => c.Id == typeId);
                if (repairType != null)
                {
                    repairType.Name = typename;
                    repairType.Price = price;
                    if (repairParentId != 0)
                    {
                        repairType.RepairProblemId = repairParentId;
                    }
                    RepairProblemDetailService.Change(repairType);
                }
            }
            else
            {
                repairType.Name = typename;
                repairType.CreateTime = DateTime.Now;
                repairType.Price = price;
                repairType.RepairProblemId = repairParentId;
                repairType.IsAbort = false;                             //修改完改回
                var last_sequence = RepairProblemDetailService.GetAll().Where(a => a.RepairProblemId == repairParentId).OrderByDescending(a => a.Sequence);
                if(last_sequence.Count() > 0)
                {
                 repairType.Sequence = last_sequence.FirstOrDefault().Sequence++;

                }
                else
                {
                    repairType.Sequence = 1;
                }
                RepairProblemDetailService.Add(repairType);
            }
            ShowMsg("操作成功", MessageItemType.Right);
            return Redirect("/admin/repair/editProblemDetail?typeId=" + typeId + "&remain="+repairParentId);
        }

        public ActionResult DeleteProblemDetail()
        {
            var typeId = Query.Value("typeId").ToInt();
            if (typeId > 0)
            {
                RepairProblemDetailService.Remove(typeId);
                ShowMsg("操作成功!", MessageItemType.Right);
                return Redirect("/admin/repair/ProblemDetail");
            }
            ShowMsg("操作失败,参数不正确!", MessageItemType.Error);
            return Redirect("/admin/repair/ProblemDetail");
        }
        #endregion

    }
}